package it.fuesi.synotools.services.tasks.download;

import android.app.Activity;
import android.content.Context;

import it.fuesi.synotools.activities.DownloadTaskDetailActivity;
import it.fuesi.synotools.model.base.VoidDataObject;
import it.fuesi.synotools.model.base.VoidResponse;
import it.fuesi.synotools.model.download.DownloadStationClientParameters;
import it.fuesi.synotools.services.cache.CacheService;
import it.fuesi.synotools.services.client.DownloadStationClient;
import it.fuesi.synotools.services.client.ServiceClientFactory;
import it.fuesi.synotools.services.tasks.base.BaseTask;

/**
 * Created by pascotto on 17/10/14.
 */
public class PauseDownloadTask extends BaseTask<VoidResponse, VoidDataObject, DownloadStationClientParameters> {

    private static final String TAG = PauseDownloadTask.class.getName();

    public PauseDownloadTask(Context context){
        super(context);
    }


    @Override
    protected String getCacheKey() {
        return null;
    }

    @Override
    public VoidResponse run(DownloadStationClientParameters params)  throws Exception{
            publishProgress("Pausing download task");
            final DownloadStationClient client = ServiceClientFactory.getClient(DownloadStationClient.class);

            return client.pause(params.getTaskId());

    }

    @Override
    protected void onPostExecute(VoidResponse voidResponse) {
        super.onPostExecute(voidResponse);

        if(voidResponse != null && voidResponse.isSuccess()){
            CacheService.expireDownloadStationCache(getContext());
            DownloadTaskDetailActivity activity = (DownloadTaskDetailActivity)getContext();
            activity.setResult(Activity.RESULT_OK);
            activity.finish();
        }
    }
}
